//
//  VCBgmManger.m
//  VideoCollage
//
//  Created by Mac on 2018/6/20.
//  Copyright © 2018年 stoneobs.qq.com. All rights reserved.
//

#import "VCBgmManger.h"
#import "NSBundle+STSystemTool.h"
#define mdeidType @"mp3"
@interface VCBgmManger()
@property(nonatomic, strong) AVPlayer           *playerLayer                     ;/**< 音乐播放 */
@end
@implementation VCBgmManger
+ (VCBgmManger *)manger{
    static VCBgmManger* deflut = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        deflut = [VCBgmManger new];
    });
    return deflut;
}
- (void)moveToDocument{
    NSArray * array = @[@"Domineering",@"Sugar Zone",@"Alive",@"Peaceful",@"Happy",
                        @"Funny",@"piano",@"Little Pico",@"Cheerful",@"Lively",
                        @"Lovely",@"Bird calling",@"Humor",@"Melodious",@"Hip-hop",
                        @"Excited"];
    [NSBundle st_createFolderFormDocumentWithFatherPath:nil folderName:@"music"];
#if DEBUG
    NSLog(@"沙盒-%@",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]);
#endif

    for (int i = 0 ; i < array.count; i ++) {
        NSString * name = array[i];
        NSString * existUrl = [[NSBundle mainBundle] pathForResource:name ofType:mdeidType];
        NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
        NSString * docImageUrl = [NSString stringWithFormat:@"%@/music/%@.%@",docDir,name,mdeidType];
        
        BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:docImageUrl];
        if (!isExist) {
            //不存在 ，移动
            [[NSFileManager defaultManager] copyItemAtPath:existUrl
                                                    toPath:docImageUrl
                                                     error:nil];
        }
        
    }
    
}
- (NSString*)urlWithName:(NSString *)name{
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString * docImageUrl = [NSString stringWithFormat:@"%@/music/%@.%@",docDir,name,mdeidType];
    BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:docImageUrl];
    if (isExist) {
        return docImageUrl;
    }
    return @"";
}
- (NSArray *)allBgm{

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString * path = [NSString stringWithFormat:@"%@/%@",NSBundle.st_documentDirectoryPath,@"music"];
    NSArray *tempFileList = [[NSArray alloc] initWithArray:[fileManager contentsOfDirectoryAtPath:path error:nil]];
    return tempFileList.copy;
}
+ (NSString *)realPath:(NSString *)music{
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString * docImageUrl = [NSString stringWithFormat:@"%@/music/%@",docDir,music];
    return docImageUrl;
}

- (void)playWithPath:(NSString *)path{
    NSURL *videoURL = [NSURL fileURLWithPath:path];
    
    [self.playerLayer pause];
    self.playerLayer = nil;
    AVPlayer *player = [AVPlayer playerWithURL:videoURL];
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayAndRecord
             withOptions:AVAudioSessionCategoryOptionMixWithOthers
                   error:nil];
    [session setCategory:AVAudioSessionCategoryPlayback error:NULL];
    [session setActive:YES error:NULL]; 
    self.playerLayer = player;
    [player play];
}
- (void)pause{
    [self.playerLayer pause];
    self.playerLayer = nil;
}
@end
